﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BUS;
using DTO;
using System.Xml;

namespace WebRaoVat
{
    public partial class SuaBaiRaoVat : System.Web.UI.Page
    {
        public int maTinRaoVatCanSua = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["TrangHienTai"] = "SuaBaiRaoVat.aspx";

            if (Session["NguoiDungDangDangNhap"] == null || Session["NguoiDungDangDangNhap"].ToString().Length <= 0)
            {
                Response.Redirect("Default.aspx");
            }

            if (!IsPostBack)
            {
                Session["MaNgonNgu"] = Config._MANGONNGU.ToString();

                TinRaoVatDTO tinRaoVatCanSua = null;
                try
                {
                    maTinRaoVatCanSua = int.Parse(Session["MaTinRaoVatDuocChon"].ToString());
                    tinRaoVatCanSua = TinRaoVatBUS.getAlongCode_MaTinRaoVat(maTinRaoVatCanSua);
                }
                catch (Exception) { }

                if (tinRaoVatCanSua != null)
                {
                    txtTenTin.Text = tinRaoVatCanSua.TenTinRaoVat;
                    txtGia.Text = tinRaoVatCanSua.GiaTien.ToString();
                    DropDownDiaDiem.Text = DiaDiemBUS.getTen(tinRaoVatCanSua.MaDiaDiem);
                    CKEditorControl1.Text = tinRaoVatCanSua.NoiDungTinRaoVat;
                    DropDownListChuyenMuc.Text = DanhMucConBUS.getTenDanhMuc(tinRaoVatCanSua.MaDanhMucCon);
                    //DropDownListThoiHanLuuTin.Text = tinRaoVatCanSua.ThoiGianUp.ToString();
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            Page.Theme = ((XmlElement)XL_XML.ReadNodeSingle(XL_XML.ReadDocumentFile(Server.MapPath("Gui.xml")),
            "/GIAODIEN/THEME")).GetAttribute("ten");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string noiDung = CKEditorControl1.Text;
            if ("" == noiDung)
                return;

            string tenTinRaoVat = txtTenTin.Text;
            int gia = 0;
            try
            {
                gia = int.Parse(txtGia.Text);
            }
            catch (Exception)
            {
            }

            string thumbnail = "";
            if ("" != FileUploadHinhDaiDien.PostedFile.FileName)
            {
                string duongDan = Server.MapPath("~/Pictures");
                string tenthat = Guid.NewGuid().ToString() + "." + FileUploadHinhDaiDien.PostedFile.FileName.Split('.')[1];
                duongDan += "/" + tenthat;
                FileUploadHinhDaiDien.PostedFile.SaveAs(duongDan);

                int _count = duongDan.LastIndexOf("\\");
                thumbnail = duongDan.Remove(0, _count + 1);
            }

            int ngonNgu = Config._MANGONNGU;
            int diaDiem = DiaDiemBUS.getMa(DropDownDiaDiem.Text);
            int maDanhMuc = DanhMucConBUS.getMa(DropDownListChuyenMuc.Text);
            //int thoiHanLuuTin = int.Parse(DropDownListThoiHanLuuTin.Text);

            TinRaoVatDTO tinRaoVat = new TinRaoVatDTO();
            tinRaoVat.MaTinRaoVat = maTinRaoVatCanSua;
            tinRaoVat.TenTinRaoVat = tenTinRaoVat;
            if(thumbnail!="")
                tinRaoVat.Thumbnail = thumbnail;
            tinRaoVat.GiaTien = gia;
            tinRaoVat.NoiDungTinRaoVat = noiDung;
            tinRaoVat.ThoiGianDang = DateTime.Now;
            tinRaoVat.SoLanXem = 0;
            tinRaoVat.MaDiaDiem = diaDiem;
            tinRaoVat.MaNgonNgu = ngonNgu;
            tinRaoVat.MaNguoiDung = int.Parse(Session["manguoidung"].ToString());
            tinRaoVat.MaDanhMucCon = maDanhMuc;
            tinRaoVat.Deleted = 0;
            tinRaoVat.ThoiGianUp = DateTime.Now;

            if (TinRaoVatBUS.Update(tinRaoVat))
            {
                Response.Redirect("NguoiDung.aspx"); 
            }
        }
    }
}
